home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 136 < prev    next >
Internet Message Format  |  1996-08-06  |  3KB

  1. Path: lyra.csx.cam.ac.uk!nmm1
  2. From: nmm1@cus.cam.ac.uk (Nick Maclaren)
  3. Newsgroups: comp.std.c
  4. Subject: Re: atexit() and return from main()
  5. Date: 18 Jan 1996 08:15:48 GMT
  6. Organization: University of Cambridge, England
  7. Message-ID: <4dkvjk$6q5@lyra.csx.cam.ac.uk>
  8. References: <4dhfja$j50@fg70.rz.uni-karlsruhe.de> <4diicg$i9e@lyra.csx.cam.ac.uk> <821923074snz@genesis.demon.co.uk>
  9. NNTP-Posting-Host: bootes.cus.cam.ac.uk
  10.  
  11. In article <821923074snz@genesis.demon.co.uk>,
  12. Lawrence Kirby  <fred@genesis.demon.co.uk> wrote:
  13. >In article <4diicg$i9e@lyra.csx.cam.ac.uk>
  14. >           nmm1@cus.cam.ac.uk "Nick Maclaren" writes:
  15. >
  16. >>In article <4dhfja$j50@fg70.rz.uni-karlsruhe.de>, ig25@fg70.rz.uni-karlsruhe.de
  17. >> (Thomas Koenig) writes:
  18. >>|> I don't see a problem when I replace the 'return 0' above with
  19. >>|> an 'exit(0)'.
  20. >>
  21. >>I do.  exit() may unwind the stack before calling the atexit()
  22. >>functions, and that is actually the way that most C run-time
  23. >>systems implement close down.
  24. >
  25. >Implementations that do that are non-conforming. According to 7.10.4.3:
  26. >
  27. >"First, all functions registered by the atexit function are called, in
  28. > the reverse order of registration."
  29. >
  30. >There is no license given to destroy any existing objects before that happens.
  31.  
  32. Hang on.  The question was whether there was likely to be a problem.
  33. There is.  The fact that such implementations are non-conformant is
  34. not a denial of that fact.
  35.  
  36. I can remember discussing this very point at great length with other
  37. people, and we came to the conclusion that the standard was confusing
  38. at best.  The clarification mentioned by Clive Feather makes it much
  39. clearer that exit() is NOT supposed to unwind the stack before calling
  40. the atexit() functions.
  41.  
  42. But you are making a serious mistake in assuming that run-time systems
  43. can be implemented to allow everything that the standard permits by
  44. omission.  They can't.  In THIS case, they can usually preserve the
  45. stack before calling the atexit() functions.
  46.  
  47.  
  48. However, for the advanced paper, think about the following problems:
  49.  
  50. What is the effect of an atexit() function calling exit()?  Does it
  51. restart the atexit() queue, carry on as if the function had returned
  52. or does it abandon the atexit() queue?
  53.  
  54. I chose to do the second, as by far the most user-friendly, but I
  55. would NEVER write serious code that relied upon that action!
  56.  
  57. main() calls setjmp() and then calls exit().  An atexit() function
  58. calls longjmp() and returns to main().  This repeats (or does it?)
  59.  
  60. This is ALSO permitted by omission, but a good run-time system will
  61. trap it, issue an error message and stop without calling more atexit()
  62. functions.
  63.  
  64.  
  65. Nick Maclaren,
  66. University of Cambridge Computer Laboratory,
  67. New Museums Site, Pembroke Street, Cambridge CB2 3QG, England.
  68. Email:  nmm1@cam.ac.uk
  69. Tel.:  +44 1223 334761    Fax:  +44 1223 334679
  70.